package ro.msg.quiz.repository;

/**
 * Author: Vlad
 */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * This class extends Question class. Multiple choice question is a particular
 * type of question. It has one or more correct answers.
 */
@XmlRootElement(name = "MultipleChoiceQ")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class MultipleChoiceQ extends Question {

	private int noOfOptions;
	// options is an arraylist which contains strings with the options for this
	// question
	private ArrayList<String> options = new ArrayList<String>();
	// correctOptions is an Arraylist which contains the indexes of the
	// correctanswers
	private ArrayList<Integer> correctOptions = new ArrayList<Integer>();
	private static final int type = ClassType.MULTIPLE_CHOICE_QUESTION
			.getCode();
	private static String dateFormatString = "dd.MM.yyyy";

	/**
	 * Constructor
	 */
	public MultipleChoiceQ() {
		this.setId(-1);
		this.setType(2);
		this.setText(null);
		this.setNoOfOptions(0);
		this.setOptions(null);
		this.setCorrectOptions(null);

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            The id of multiple choice question.
	 * @param text
	 *            The the of multiple choice question.
	 * @param noOfOptions
	 *            The number of options of multiple choice question.
	 * @param options
	 *            The options of multiple choice question.
	 * @param correctOptions
	 *            The correct options of multiple choice question.
	 */
	public MultipleChoiceQ(int id, String text, int noOfOptions,
			ArrayList<String> options, ArrayList<Integer> correctOptions) {
		this.setId(id);
		this.setType(2);
		this.setText(text);
		this.setNoOfOptions(noOfOptions);
		this.setOptions(options);
		this.setCorrectOptions(correctOptions);

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * 
	 * @return Returns the number of options.
	 */
	@XmlElement(name = "noOfOptions")
	public int getNoOfOptions() {
		return noOfOptions;
	}

	/**
	 * Sets the number of options for multiple choice question.
	 * 
	 * @param noOfOptions
	 *            The number of options to be set.
	 */
	public void setNoOfOptions(int noOfOptions) {
		this.noOfOptions = noOfOptions;

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * 
	 * @return Returns the list of options.
	 */
	@XmlElementWrapper(name = "options")
	@XmlElement(name = "option")
	@Override
	public ArrayList<String> getOptions() {
		return options;
	}

	/**
	 * Sets the list of options for multiple choice question.
	 * 
	 * @param options
	 *            The list of options to be set.
	 */
	public void setOptions(ArrayList<String> options) {
		this.options = options;

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * 
	 * @return Returns the list of correct answers for multiple choice question.
	 */
	@XmlElementWrapper(name = "correctAnswers")
	@XmlElement(name = "correctAnswer")
	public ArrayList<Integer> getCorrectOptions() {
		return correctOptions;
	}

	/**
	 * @return Returns the correct answer
	 */
	@Override
	public ArrayList<String> getAnswers() {
		ArrayList<String> answer = new ArrayList<String>();
		for (Integer i : correctOptions)
			answer.add(Integer.toString(i));
		return answer;
	}

	/**
	 * Sets the list of correct options for multiple choice question.
	 * 
	 * @param correctOptions
	 *            The list of correct options to be set.
	 */
	public void setCorrectOptions(ArrayList<Integer> correctOptions) {
		this.correctOptions = correctOptions;

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * @return Returns the type of question.
	 */
	@Override
	public int getType() {
		return type;
	}

	/**
	 * This method prints the multiple choice question.
	 */
	@Override
	public String toString() {
		return getId() + " " + getText() + " " + options.toString() + " "
				+ correctOptions.toString();
	}
}
